package com.kx.yooli.kx.wechat.service.bank.impl;

import com.alibaba.fastjson2.JSONObject;
import com.kx.yooli.kx.wechat.config.propertiesconf.CustomizedPropertyConfigurer;
import com.kx.yooli.kx.wechat.mapper.paycard.BankInfoMapper;
import com.kx.yooli.kx.wechat.mapper.paycard.WechatPaycardMapper;
import com.kx.yooli.kx.wechat.mapper.user.UserInfoMapper;
import com.kx.yooli.kx.wechat.po.paycard.WechatPaycard;
import com.kx.yooli.kx.wechat.po.result.AjaxResult;
import com.kx.yooli.kx.wechat.po.user.UserInfo;
import com.kx.yooli.kx.wechat.service.bank.BankService;
import com.kx.yooli.kx.wechat.service.impl.UserServiceImpl;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BankServiceImpl implements BankService {

    private final Logger log = LoggerFactory.getLogger(BankServiceImpl.class);

    @Resource
    private WechatPaycardMapper wechatPaycardMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private BankInfoMapper bankInfoMapper;

    @Override
    public AjaxResult getBankList(String o) {
        String code = JSONObject.parseObject(o).getString("idNo");
        if (StringUtils.isEmpty(code)){
            return AjaxResult.error("用户信息不能为空");
        }
        log.info("银行卡数据查询开始{}",code);
        return AjaxResult.success(bankInfoMapper.getBankInfoListByCardNo(code));
    }
    @Transactional
    @Override
    public AjaxResult updateBank(WechatPaycard o) {
        if (StringUtils.isEmpty(o.getcId())||o.getcId().equals("0")){
            return AjaxResult.error("银行卡信息有误");
        }
        if (StringUtils.isEmpty(o.getcCardNo())){
            return AjaxResult.error("银行卡号不能为空");
        }
        if (StringUtils.isEmpty(o.getcIdNo())){
            return AjaxResult.error("身份证号不能为空");
        }
        try {
            log.info("银行卡数据更新开始{}",o);
            UserInfo userInfo = userInfoMapper.selectByCertNo(o.getcIdNo());
            if (userInfo == null){
                return AjaxResult.error("用户信息不存在");
            }
            List<WechatPaycard> wechatPaycards = wechatPaycardMapper.getPaycardListByCardNo(o.getcCardNo());
            if (wechatPaycards.size() > 0){
                return AjaxResult.error("银行卡号已存在");
            }
            if (wechatPaycardMapper.updateUserBank(o)){
                return AjaxResult.success();
            }
            return AjaxResult.error("数据更新失败");
        }catch (Exception e){
            log.error("银行卡数据更新失败",e);
            return AjaxResult.error("网络请求错误");
        }
    }
    @Transactional
    @Override
    public AjaxResult insertBank(WechatPaycard o) {
        if (StringUtils.isEmpty(o.getcCardNo())){
            return AjaxResult.error("银行卡号不能为空");
        }
        if (StringUtils.isEmpty(o.getcIdNo())){
            return AjaxResult.error("身份证号不能为空");
        }
        try {
            log.info("银行卡数据更新开始{}",o);
            UserInfo userInfo = userInfoMapper.selectByCertNo(o.getcIdNo());
            if (userInfo != null){
                return AjaxResult.error("用户信息已存在");
            }
            if (userInfoMapper.selectCountByMobile(o.getcMobile()) > 0){
                return AjaxResult.error("手机号已存在");
            }
            List<WechatPaycard> wechatPaycards = wechatPaycardMapper.getPaycardListByCardNo(o.getcCardNo());
            if (wechatPaycards.size() > 0){
                return AjaxResult.error("银行卡号已存在");
            }
            userInfo = new UserInfo();
            userInfo.setMobile(o.getcMobile());
            userInfo.setCertNo(o.getcIdNo());
            userInfo.setName(o.getcHolderName());
            userInfo.setCertified(0);
            userInfoMapper.insert(userInfo);
            if (wechatPaycardMapper.insert(o) > 0){
                return AjaxResult.success("数据添加成功",userInfo);
            }
            return AjaxResult.error("数据更新失败");
        }catch (Exception e){
            log.error("银行卡数据更新失败",e);
            return AjaxResult.error("网络请求错误");
        }
    }
}
